//
//  PurchaseInforViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/3.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "PurchaseInforViewModel.h"
#import "PurchaseInforTableViewHeaderView.h"
#import "PurchasePhoneTableViewHeaderView.h"
#import "NoDataFootTableViewHeaderFooterView.h"
#import "PurchaseListTableViewHeaderFooterView.h"
#import "ConfirmTopTimeView.h"
#import "PurchaseGoodsTableViewCell.h"
#import "PurchaseOfferTableViewCell.h"
#import "RequirementsTableViewCell.h"
#import "ConfirmInformationTableViewCell.h"
#import "PurchaseBottomView.h"
#import "PurchaseInforFootView.h"
#import "PurchaseInformationModel.h"

@interface PurchaseInforViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView *selfTableView;
}
@property (nonatomic, assign) BOOL openBool;
// 报价列表model
@property (nonatomic, strong) PurchaseInformationModel *quotationListModel;

@end

@implementation PurchaseInforViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView
{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.openBool = NO;
        tableView.y = tableView.y + kHeightProportion_In_iPhoneTriploid(30);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(95);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.dataSource = self;
        tableView.delegate = self;
        [tableView registerClass:[PurchaseInforTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:@"PurchaseInforTableViewHeaderView"];
        [tableView registerClass:[PurchasePhoneTableViewHeaderView class] forHeaderFooterViewReuseIdentifier:@"PurchasePhoneTableViewHeaderView"];
        [tableView registerClass:[NoDataFootTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"NoDataFootTableViewHeaderFooterView"];
        [tableView registerClass:[PurchaseListTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"PurchaseListTableViewHeaderFooterView"];
        tableView.tableFooterView = [self createFootView];
        [superView addSubview:tableView];
        selfTableView = tableView;
        [self createTopView:superView];
        
        PurchaseBottomView *bottomView = [[PurchaseBottomView alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(65)) inSuperView:superView];
        
        [self requestPurchaseList];
        [self requestQuotationList];
    }
    return self;
}

- (UIView *)createFootView
{
    PurchaseInforFootView *footView = [[PurchaseInforFootView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(150))];
    return footView;
}

- (void)createTopView:(UIView *)s_View
{
    ConfirmTopTimeView *headerView = [[ConfirmTopTimeView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(30)) inSuperView:s_View];
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger cellNum = 0;
    switch (section) {
        case 0:{
            cellNum = 3;
        }
            break;
        case 1:{
            if (self.openBool) {
                cellNum = 2;
            }
        }
            break;
        case 2:{
            if (self.quotationListModel) {
                cellNum = self.quotationListModel.data.infos.count;
            }
        }
            break;
            
        default:
            break;
    }
    return cellNum;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cellHeight;
    switch (indexPath.section) {
        case 0:{
            cellHeight = kHeightProportion_In_iPhoneTriploid(110);
        }
            break;
        case 1:{
            if (indexPath.row == 0) {
                cellHeight = kHeightProportion_In_iPhoneTriploid(80);
            } else {
                cellHeight = kHeightProportion_In_iPhoneTriploid(180);
            }
        }
            break;
        case 2:{
            if (indexPath.row == 0) {
                cellHeight = kHeightProportion_In_iPhoneTriploid(137);
            } else {
                cellHeight = kHeightProportion_In_iPhoneTriploid(200);
            }
        }
            break;
            
        default:
            cellHeight = 0.01;
            break;
    }
    return cellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BaseTableViewCell *cell;
    switch (indexPath.section) {//
        case 0:{
            PurchaseGoodsTableViewCell *cell_goods = [PurchaseGoodsTableViewCell cellWithTableView:tableView];
            cell = cell_goods;
        }
            break;
        case 1:{
            if (indexPath.row == 0) {
                RequirementsTableViewCell *cell_rement = [RequirementsTableViewCell cellWithTableView:tableView];
                cell = cell_rement;
            } else {
                ConfirmInformationTableViewCell *cell_confirm = [ConfirmInformationTableViewCell cellWithTableView:tableView];
                cell = cell_confirm;
            }
        }
            break;
        case 2:{
            PurchaseOfferTableViewCell *cell_offer = [PurchaseOfferTableViewCell cellWithTableView:tableView];
            cell = cell_offer;
        }
            break;
            
        default:
            break;
    }
    if (cell == nil) {
        cell = [BaseTableViewCell cellWithTableView:tableView];
    }
    return cell;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *headerView;
    if (section == 1) {
        PurchasePhoneTableViewHeaderView *headerView_phone = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PurchasePhoneTableViewHeaderView"];
        kWeakSelf(self);
        headerView_phone.Block_OpenAndCloseAction = ^(BOOL closeBool) {
            weakself.openBool = closeBool;
            NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:section];
            [tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        };
        headerView = headerView_phone;
    } else {
        PurchaseInforTableViewHeaderView *headerView_Title = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"PurchaseInforTableViewHeaderView"];
        headerView = headerView_Title;
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return kHeightProportion_In_iPhoneTriploid(35);
    } else {
        return kHeightProportion_In_iPhoneTriploid(47);
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NoDataFootTableViewHeaderFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"NoDataFootTableViewHeaderFooterView"];
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return kHeightProportion_In_iPhoneTriploid(10);
    } else {
        return 0.01;
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(purInformationDidSelectRowAtIndexPath:)]) {
        [self.delegate purInformationDidSelectRowAtIndexPath:indexPath];
    }
}


#pragma mark - 请求接口
// 求购明细
- (void)requestPurchaseList
{
//    NSDictionary *param = @{@"id" : koString(self.orderStr)};
    NSDictionary *param = @{@"id" : @"20"};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyOrderBuyerGoodsDetail] lodingView:nil lodingTextString:@"" isLoding:YES parameters:param success:^(id  _Nullable response) {
       
    } failure:^(NSError * _Nullable error) {
     
    }];
}

// 时候到的报价
- (void)requestQuotationList
{
    @weakify(self)
    _service_RecommendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        RACSignal * requestSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSMutableDictionary * parDic = (NSMutableDictionary *)input;
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kMyOrderBuyerGoodsQuotationList] lodingView:nil lodingTextString:@"" isLoding:YES parameters:parDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        return [requestSignal map:^id _Nullable(id  _Nullable value) {
            self.quotationListModel = [PurchaseInformationModel yy_modelWithJSON:value];
            if (self.quotationListModel.code == 0) {
                [self->selfTableView reloadData];
            }
            return self.quotationListModel;
        }];
    }];
}

@end
